package Json_demo;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class Json_Save_ListHasmap {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//khoi tao 1 hashmap (key,value) chua 4 obj Teacher
		List<HashMap<Integer, Teacher>> HashmapList = new ArrayList<HashMap<Integer , Teacher>>();
		HashMap<Integer, Teacher> HashmapTe = new HashMap<>();
		HashMap<Integer, Teacher> HashmapTe01 = new HashMap<>();
//khoi tao 4 giao vien dua vao hash map
		Teacher Gv01 = new Teacher();
		Gv01.setRollNo("gv01");
		Gv01.setFullName("Tran a");
		Gv01.setMon("Toan");
		Gv01.setDiemSo(8.0);
		Teacher Gv02 = new Teacher();
		Gv02.setRollNo("gv01");
		Gv02.setFullName("Tran f");
		Gv02.setMon("Toan");
		Gv02.setDiemSo(9.0);
		Teacher Gv03 = new Teacher();
		Gv03.setRollNo("gv01");
		Gv03.setFullName("Tran e");
		Gv03.setMon("Toan");
		Gv03.setDiemSo(7.0);
		Teacher Gv04 = new Teacher();
		Gv04.setRollNo("gv01");
		Gv04.setFullName("Tran d");
		Gv04.setMon("Toan");
		Gv04.setDiemSo(5.0);

		Teacher Gv06 = new Teacher();
		Gv04.setRollNo("gv05");
		Gv04.setFullName("Tran f");
		Gv04.setMon("Toan");
		Gv04.setDiemSo(7.70);

		Teacher Gv05 = new Teacher();
		Gv04.setRollNo("gv06");
		Gv04.setFullName("Tran e");
		Gv04.setMon("Toan");
		Gv04.setDiemSo(9.0);
HashmapTe.put(0, Gv01);
HashmapTe.put(1, Gv02);
HashmapTe.put(2, Gv03);
HashmapTe.put(3, Gv04);
HashmapTe01.put(0, Gv01);
HashmapTe01.put(1, Gv02);
HashmapList.add(HashmapTe);
HashmapList.add(HashmapTe01);
//bat cua so luu files
JFileChooser objFile = new JFileChooser();
objFile.setCurrentDirectory(new java.io.File("."));
objFile.setDialogTitle("Chọn file: ");
objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
String[] EXTENSION = new String[] { "json" };
FileNameExtensionFilter filter = new FileNameExtensionFilter("JSON files", EXTENSION);
objFile.setFileFilter(filter);
objFile.setMultiSelectionEnabled(false);

int returnVal = objFile.showSaveDialog(null);
String ParentPath = "";
String FileName = "";
if (returnVal == JFileChooser.APPROVE_OPTION) {
    // ParentPath = objFile.getSelectedFile().getParent();
    FileName = objFile.getSelectedFile().getPath();
// khai bao thu vien Json
    ObjectMapper objma = new ObjectMapper();
// dua du lieu Hashmap vao 
    try {
		objma.writeValue(new File(FileName), HashmapList);
	} catch (JsonGenerationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (JsonMappingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    
	}
	}
}
